#ifndef MAIN_WINDOW_H
#define MAIN_WINDOW_H

#include <QTimer>
#include <QWidget>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QPlainTextEdit>
#include <QCheckBox>
#include <QLabel>

#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

#include "hex_text_edit.h"
#include "hex_line_edit.h"
#include "send_panel.h"

QT_USE_NAMESPACE

class MainWindow : public QWidget
{
    Q_OBJECT

private:
    QSerialPort serialPort;
    QPushButton *openButton;
    QComboBox *serialPortBox;
    int customBaudRate;
    QComboBox *baudRateBox;
    QComboBox *dataBitsBox;
    QComboBox *stopBitsBox;
    QComboBox *parityBox;
    QComboBox *flowControlBox;
    QPlainTextEdit *rTextShowEdit;
    HexTextEdit *rHexShowEdit;
    QByteArray sendFileData;
    QFile sendFile;
    QPushButton *sendFileButton;
    QFile saveFile;
    QPushButton *saveFileButton;
    QLineEdit *sendFileEdit;
    QLineEdit *sTextShowEdit;
    HexLineEdit *sHexShowEdit;
    QCheckBox *dtrCheckBox;
    QCheckBox *rtsCheckBox;
    bool showHex;
    QCheckBox *showHexCheckBox;
    bool sendEnter;
    QCheckBox *sendEnterCheckBox;
    bool sendNewLine;
    QCheckBox *sendNewLineCheckBox;
    bool sendHex;
    QCheckBox *sendHexCheckBox;
    bool isSaving;
    QTimer sendTimer;
    QCheckBox *intervalBox;
    QLineEdit *intervalLineEdit;
    quint64 recvCount;
    quint64 sendCount;

    QLabel *serialPortInfo;
    QLabel *recvLabel;
    QLabel *sendLabel;
    QWidget *expandPanel;
    bool showExpandPanel;
private:
    QLayout *drawDataShowPanel(void);
    QLayout *drawConfigPanel(void);
    QLayout *drawSendPanel(void);
    QWidget *drawExpandPanel(void);
    QLayout *drawStatusPanel(void);

private slots:
    void setExpandPanelEnabled(bool enabled);
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
public slots:
    void openSerialPortSlotFunc(void);
    void refreshSerialPortBoxSlotFunc(void);
    void clearRShowEditSlotFunc(void);
    void serialPortEditTextChangedSlotFunc(const QString &text);
    void serialPortRecvSlotFunc(void);
    void serialPortSendSlotFunc(void);
    void chooseSendFileSlotFunc(void);
    void startSendFileSlotFunc(void);
    void sendFileSlotFunc(qint64 len);
    void saveFileSlotFunc(void);
    void setShowHexSlotFunc(bool showHex);
    void setSendEnterSlotFunc(bool enter);
    void setSendChangeLineSlotFunc(bool changeLine);
    void setSendHexSlotFunc(bool sendHex);
    void serialPortChangedSlotFunc(QString text);
    void baudRateChangedSlotFunc(int index);
    void dataBitsChangedSlotFunc(int index);
    void stopBitsChangedSlotFunc(int index);
    void parityChangedSlotFunc(int index);
    void flowControlChangedSlotFunc(int index);
    void intervalSendSlotFunc(bool check);
    void openUrl(QString url);
    void setDtrValue(bool value);
    void setRtsValue(bool value);

signals:
    void sendFileSignal(qint64 len);
protected:
    bool eventFilter(QObject *obj, QEvent *e);
    void closeEvent(QCloseEvent *event);
private:
    bool openSerialPort(const QString &name);
    void closeSerialPort(void);
    void readSettings(void);
    void writeSettings(void);
private slots:
    void processSerialPortSendFinished(qint64 byteNum);
    void processSerialPortError(QSerialPort::SerialPortError error);
};

#endif // MAIN_WINDOW_H
